package com.tianqi.util;

import java.io.IOException;

import sun.misc.BASE64Decoder;

public class Base64DOer
{

	public static boolean is_base64_string(String src)
	{
		boolean is_base64 = false;
		if (src.indexOf("xxxBASE64_STARTxxx") == -1)
		{
			is_base64 = false;
		} else
		{
			is_base64 = true;
		}

		return is_base64;
	}

	public static String shift_base64_string(String src)
	{
		int begin_index = src.indexOf("xxxBASE64_STARTxxx") + "xxxBASE64_STARTxxx".length();
		int end_index = src.indexOf("xxxBASE64_ENDxxx");
		// ---- System.out.println(begin_index + " " + end_index);
		String result = src.substring(begin_index, end_index);
		return result;
	}
	
	public static String _decode (String base64_code) throws IOException
	{
		BASE64Decoder decoder = new BASE64Decoder();
		byte[] b = decoder.decodeBuffer(base64_code);
		return new String(b);
	}
	
	public static void main(String[] args) throws IOException
	{
		String src = "xxxBASE64_STARTxxxRklGQSAxNSBQU04gUFMgUFMzIFBTNCBNw7xuemVuIENvaW5zIDEwMDAuMDAwIDEwMDBrIDFNaW8gRlVUMjAxNSBVbHRpbWF0ZSBUZWFtxxxBASE64_ENDxxx";
		System.out.println(Base64DOer.is_base64_string(src));
		System.out.println(Base64DOer.shift_base64_string(src));
		System.out.println(Base64DOer._decode(Base64DOer.shift_base64_string(src)));
	}
	
}
